import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/utils/color_utils.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:flutterlib/model/activity/activity_create_request.dart';
import 'package:flutterlib/pages/activity/base_activity_create_page.dart';
import 'package:flutterlib/widgets/calendar_view.dart';
import 'package:flutterlib/widgets/editor/editor_view.dart';
import 'package:rounded_loading_button/rounded_loading_button.dart';


//兴趣活动
class Page extends BaseActivityCreatePage {
  //传入小圈id
  final int interestId;

  const Page({Key key, this.interestId}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _DeployActivityCreatePageState();
  }
}

class _DeployActivityCreatePageState extends BaseActivityCreatePageState<Page> {

  TextEditingController questionController = TextEditingController();
  final FocusNode questionFocusNode = FocusNode();
  @override
  void initState() {
    super.initState();
  }

  String get activityTitle => STATIC_STRING_CREATE_DEPLOY_ACTIVITY;

  Widget fillIntro() {
    const double inset = 20;
    return Container(
        padding: EdgeInsets.only(left: inset, right: inset),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
                padding: EdgeInsets.only(top: inset, bottom: inset),
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        STATIC_STRING_TITLE,
                        style: TextStyle(fontSize: 16),
                      ),
                      SizedBox(
                          height: 20,
                          child: Image.asset("assets/images/edit.png"))
                    ])),
            TextField(
              decoration: InputDecoration(
                  hintText: STATIC_STRING_TITLE_HINT, border: InputBorder.none),
              controller: titleController,
            ),
            Divider(
              color: Theme.of(context).dividerColor,
              height: 1.h,
            ),
            Padding(
                padding: EdgeInsets.only(top: inset, bottom: inset),
                child: Text(STATIC_STRING_TIME)),
            getSelectWidget(dateTimeFormat, () {
              unFocusTextField();
              showCalendarDialog(context, startTime, endTime, (start, end) {
                setState(() {
                  startTime = start;
                  endTime = end;
                });
              });
            }),
            SizedBox(
              height: inset,
            ),
            Text(STATIC_STRING_PERSON_NUMBER, style: TextStyle(fontSize: 16)),
            TextField(
              controller: personNumberController,
              decoration:
                  InputDecoration(hintText: "0", border: InputBorder.none),
              keyboardType: TextInputType.number,
              inputFormatters: [FilteringTextInputFormatter.digitsOnly],
            ),
            Divider(
              color: Theme.of(context).dividerColor,
              height: 1,
            ),
            Padding(
                padding: EdgeInsets.only(top: inset, bottom: inset),
                child: Text(STATIC_STRING_QUESTION,
                    style: TextStyle(fontSize: 16))),
            Container(
                decoration: BoxDecoration(
                    color: HexColor("#E0E0E0"),
                    borderRadius: BorderRadius.circular(4)),
                height: 150,
                child: TextField(
                  style: TextStyle(fontSize: 14),
                  controller: questionController,
                  // autofocus: true,
                  // cursorColor: Colors.green,
                  maxLines: 10,
                  decoration: InputDecoration(
                      hintText: STATIC_STRING_QUESTION_HINT,
                      contentPadding: EdgeInsets.all(5),
                      border: InputBorder.none),
                )),
            SizedBox(
              height: 10,
            ),
            Padding(
                padding: EdgeInsets.only(top: inset, bottom: inset),
                child: Text(STATIC_STRING_FILL_DESCRIPATION,
                    style: TextStyle(fontSize: 16))),
            Container(
                decoration: BoxDecoration(
                    color: HexColor("#E0E0E0"),
                    borderRadius: BorderRadius.circular(4)),
                height: 200,
                child: EditorView(
                  controller: editorController,
                )),
            SizedBox(
              height: 40,
            ),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              Checkbox(
                value: isAgree,
                onChanged: (value) {
                  setState(() {
                    isAgree = value;
                  });
                },
              ),
              Text(STATIC_STRING_AGREEMENT,
                  style: TextStyle(fontSize: 14, color: HexColor("#999999")))
            ]),
            Center(
              child: FractionallySizedBox(
                widthFactor: 156 / 375,
                child: RoundedLoadingButton(
                  color: HexColor("#333333"),
                  child: Text(
                    STATIC_STRING_CONFIRM_CREATE,
                    style: TextStyle(color: Colors.white, fontSize: 15),
                  ),
                  successColor: QuhuiColors.mainThemeColor,
                  controller: btnController,
                  onPressed: () {
                    checkConvention();
                  },
                  errorColor: QuhuiColors.textColorRed,
                ),
              ),
            ),
            SizedBox(
              height: 45,
            )
          ],
        ));
  }


  @override
  void unFocusTextField() {
    titleFocusNode.unfocus();
    personCountfocusNode.unfocus();
    descFocusNode.unfocus();
    questionFocusNode.unfocus();
  }


  @override
  ActivityCreateRequest getCreateRequestBean(String content) {
    String location;
    if(store.myPosition != null){
       location = '${store.myPosition.longitude},${store.myPosition.latitude}';
    }
    var createRequest = ActivityCreateRequest(
      aim: titleController.text,
      companyId: 0,
      cost: "0",
      description: content,
      startTime: startTime.millisecondsSinceEpoch ~/ 1000,
      endTime: endTime.millisecondsSinceEpoch ~/ 1000,
      userId: currentUser.uid,
      question: questionController.text,
      isOnline: 1,
      location: location,
      limitPeople: personNumberController.text.isEmpty
          ? 0
          : personNumberController.text.toInt(),
      logo: logoRemote,
      groupId: widget.interestId,
      type: 1,
      notifyConfig: "1",
    );
    return createRequest;
  }
}
